package com.ruoyi.CheryEcos.controller;

import com.ruoyi.CheryEcos.domain.CheryEcos;
import com.ruoyi.CheryEcos.service.IEcosService;
import com.ruoyi.WELD.domain.Weld;
import com.ruoyi.WELD.service.IWeldService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ECOS/ECOS")
@Api("数据湖")
public class EcosController extends BaseController {

    @Autowired
    private IEcosService iEcosService;

    @GetMapping("/list")
    public TableDataInfo list(CheryEcos cheryEcos){
        startPage();
        List<CheryEcos> list=iEcosService.selectCheryEcosList(cheryEcos);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo (@PathVariable Long id){
        return success(iEcosService.selectCheryEcosById(id));
    }

    @PostMapping
    public AjaxResult add(@Validated @RequestBody CheryEcos cheryEcos){
       return toAjax(iEcosService.insertEcos(cheryEcos));
    }

    @PutMapping
    public AjaxResult update(@Validated @RequestBody CheryEcos cheryEcos){
        return toAjax(iEcosService.updateEcos(cheryEcos));
    }

//    @DeleteMapping("/{id}")
//    public AjaxResult delete(@PathVariable Long id){
//        return toAjax(iEcosService.deleteEcosById(id));
//    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids){
        return toAjax(iEcosService.deleteEcosByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CheryEcos> util = new ExcelUtil<CheryEcos>(CheryEcos.class);
        util.importTemplateExcel(response, "数据");
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CheryEcos> util = new ExcelUtil<CheryEcos>(CheryEcos.class);
        List<CheryEcos> ecosList = util.importExcel(file.getInputStream());
        String message = iEcosService.importEcos(ecosList, updateSupport);
        return success(message);
    }

    @PostMapping("/export")
    public void export(Long[] ids,HttpServletResponse response)
    {
        List<CheryEcos> list = iEcosService.selectCheryEcosByIds(ids);
        ExcelUtil<CheryEcos> util = new ExcelUtil<CheryEcos>(CheryEcos.class);
        util.exportExcel(response, list, "数据");
    }

}
